﻿@model System.Object
@using MvcTemplates;

@helper RenderLabel( ModelMetadata property )
{    
    if ( !string.IsNullOrEmpty( Html.Label( property.PropertyName ).ToHtmlString() ) ) 
    {
        var labelAttribute = property.GetTemplateData<LabelAttribute>() ?? new LabelAttribute();
        var labelContainerAttribute = property.GetTemplateData<LabelContainerAttribute>(); 
                
        if ( !labelAttribute.PutInFieldContainer ) 
        {
            if ( labelContainerAttribute == null )
            {
                @Html.C( c => c.LabelContainer, TagRenderMode.StartTag, propertyName: property.PropertyName )
            }
            else 
            {
                @labelContainerAttribute.GetStartTagAndInnerContent( Html, property.PropertyName );
            }
            @Environment.NewLine            
        }
                       
        @Html.Label( property.PropertyName )
        @Environment.NewLine

        if ( property.IsRequired && !labelAttribute.HideRequiredTag ) 
        {
            @Html.C( c => c.RequiredTagBuilder, propertyName: property.PropertyName )
            @Environment.NewLine
        }            
        
        if ( !labelAttribute.PutInFieldContainer ) 
        {
            if ( labelContainerAttribute == null )
            {
                @Html.C( c => c.LabelContainer, TagRenderMode.EndTag )
            }
            else 
            {
                @labelContainerAttribute.GetEndTag( Html )
            }            
            @Environment.NewLine
        }
    }
}


@if ( ViewData.TemplateInfo.TemplateDepth > 1) 
{
    @ViewData.ModelMetadata.SimpleDisplayText
    @Environment.NewLine
} 
else 
{
    foreach (var property in ViewData.ModelMetadata.Properties.Where( pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited( pm ) ) )
    {
        if ( property.HideSurroundingHtml ) 
        {
            @Html.Editor( property.PropertyName )
        } 
        else 
        {
            var labelAttribute = property.GetTemplateData<LabelAttribute>() ?? new LabelAttribute();            
            
            if ( !labelAttribute.RenderAfterField && !labelAttribute.PutInFieldContainer )
            {
                @RenderLabel( property );                                                                                                    
            }                    
            
            var fieldContainerAttribute = property.GetTemplateData<FieldContainerAttribute>();
            
            if ( fieldContainerAttribute == null )
            {
                @Html.C( c => c.FieldContainer, TagRenderMode.StartTag, new { @class = labelAttribute.PutInFieldContainer ? ( labelAttribute.RenderAfterField ? "Label_PutInFieldContainer Label_RenderAfterField" : "" )  : "" }, propertyName: property.PropertyName )
            }
            else
            {
                @fieldContainerAttribute.GetStartTagAndInnerContent( Html, property.PropertyName )
            }            
                    
                if ( !labelAttribute.RenderAfterField && labelAttribute.PutInFieldContainer )
                {
                    @RenderLabel( property );            
                }
                    
                if ( property.HasTemplateData<DisplayTemplateAttribute>() )
                {
                    @Html.Display(property.PropertyName)
                }
                else
                {
                    @Html.Editor(property.PropertyName)
                }
                
                var labelAfterFieldAttribute = property.GetTemplateData<LabelAfterFieldAttribute>();
        
                if ( labelAfterFieldAttribute != null )
                {
                    @Html.Label( property.PropertyName, labelAfterFieldAttribute.Text )
                } 

                if ( labelAttribute.RenderAfterField && labelAttribute.PutInFieldContainer )
                {
                    @RenderLabel( property );
                }
                    
                var helpMessage = property.GetTemplateData<HelpMessageAttribute>();
                                                
                if ( helpMessage != null )
                {
                    @helpMessage.GetStartTagAndInnerContent( Html, property.PropertyName )                    
                        @helpMessage.Text
                    @helpMessage.GetEndTag( Html )                    
                    @Environment.NewLine
                }
                    
                @Html.ValidationMessage( property.PropertyName, Html.GetProfile().DefaultValidationMessage( ViewContext ) )
                @Environment.NewLine

            if ( fieldContainerAttribute == null )
            {
                @Html.C( c => c.FieldContainer, TagRenderMode.EndTag )
            }
            else
            {
                @fieldContainerAttribute.GetEndTag( Html )
            }
            
            @Environment.NewLine

            if ( labelAttribute.RenderAfterField && !labelAttribute.PutInFieldContainer )
            {
                @RenderLabel( property );
            }
    
            @Html.C( c => c.ClearFix, propertyName: property.PropertyName )            
        }
    }    
}
